package 回溯;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName _90子集II
 * @Description TODO
 * @Author 昝亚杰
 * @Date 2021/11/16 21:21
 * Version 1.0
 **/
public class _90子集II {
    List<List<Integer>> results = new ArrayList<>();
    List<Integer> result = new ArrayList<>();
    int[] used;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        used = new int[nums.length];
        backTrack(nums,0);
        return results;
    }
    private void backTrack(int[] nums, int start){
        results.add(new ArrayList<>(result));//妙啊！！!
        if(start >= nums.length){
            return ;
        }
        for(int i = start; i < nums.length; i++){
            if(i > 0 && nums[i] == nums[i-1] && used[i-1] == 0){
                continue;
            }
            result.add(nums[i]);
            used[i] = 1;
            backTrack(nums, i + 1);
            result.remove(result.size() -1);
            used[i] = 0;
        }
    }
}
